/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apt.professionaltag.Process;

import com.apt.tag.Connect_DB;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Bach
 */
public class CreateProfessional {

    public static boolean Create_Professional(String payname) {
        boolean re = false;
        Connection cnn = Connect_DB.getConnection();
        PreparedStatement ps = null;
        String qurey = " INSERT INTO [Matrimony].[dbo].[Professional_Types]\n"
                + "           ([Professional_Status]\n"
                + "           ,[Professinal_Name])\n"
                + "            VALUES  (1,?)";
        try {
            if (checkusername(payname, cnn)) {
                ps = cnn.prepareStatement(qurey);
                if (payname != null) {
                    ps.setString(1, payname);
                } else {
                    System.out.println("class CreatePayment Mess Insert [Matrimony].[dbo].[Professional_Types] fail ");
                    return false;
                }

                int i = ps.executeUpdate();
                if (i > 0) {
                    re = true;
                }
            } else {
                System.out.println("class CreateProfessional thong bao Ten Professional da ton tai");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Connect_DB.close(null, null, ps, cnn);
        }
        return re;
    }

    public static boolean checkusername(String name, Connection cnn) {
        boolean re = true;
        PreparedStatement ps = null;
        String qurey = "SELECT [Professional_Id]\n"
                + "      ,[Professional_Status]\n"
                + "      ,[Professinal_Name]\n"
                + "  FROM [Matrimony].[dbo].[Professional_Types] where Professinal_Name=? ";
        ResultSet rs = null;
        try {
            ps = cnn.prepareStatement(qurey);
            ps.setString(1, name);
            rs = ps.executeQuery();
            if (rs.next()) {
                re = false;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return re;
    }
}
